package top.milkbox.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.milkbox.domain.Userinfo;
import top.milkbox.service.ClickService;
import top.milkbox.utils.R;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/clicks")
public class ClickController {

    @Autowired
    private ClickService clickService;

    // TODO 涉及用户session，需要在R中设置login字段

    /**
     * 某个用户对某个菜点击量加一，用户的信息从session中获取
     *
     * @param menuId  菜单id
     * @param session 用户session
     * @return R 返回结果
     */
    @PostMapping()
    public R addClick(
            @RequestBody Integer menuId,
            HttpSession session
    ) {
        Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
        R r = new R();
        if (userinfo == null) {
            r.setFlag(false);
            r.setLogin(false);
            r.setMessage("用户未登录");
        } else {
            clickService.addClick(userinfo.getId(), menuId);
            r.setFlag(true);
            r.setLogin(true);
            r.setMessage("点击量加一成功");
        }
        return r;
    }
}
